//
//  EntryViewControlloer.m
//  EasyToo
//
//  Created by zhengzhe on 13-8-25.
//
//

#import "MapViewController.h"

@interface MapViewController ()

@end

@implementation MapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationItem setTitle:@"周边"];

    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self.view addSubview:_mapView];
//    //
////    UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0, 80, 320, 20)] autorelease];
////    [button setBackgroundColor:[UIColor redColor]];
////    [button addTarget:self action:@selector(onClickButton) forControlEvents:UIControlEventTouchUpInside];
////    [self.view addSubview:button];
    [self startLocation:nil];
//    [_mapView setZoomLevel:16];
//
////
////    _search = [[BMKSearch alloc] init];
////    _search.delegate = self;
//    
//    NSLog(@"start search");
    
}

- (void)dealloc
{
    [_mapView release];
    _mapView = nil;
    [_search release];
    _search = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;
}

- (void)onClickButton
{
    BOOL isSuccess = [_search poiSearchInCity:@"深圳" withKey:@"皇庭居" pageIndex:0];
//    BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc] init];
//    CLLocationCoordinate2D coor;
//    coor.latitude = 80.915;
//    coor.longitude = 116.404;
//    annotation.coordinate = coor;
//    annotation.title = @"这里是北京";
//    [_mapView addAnnotation:annotation];
//    [_mapView setCenterCoordinate:coor];

}

//普通态
-(IBAction)startLocation:(id)sender
{
    NSLog(@"进入普通定位态");
    _mapView.showsUserLocation = NO;//先关闭显示的定位图层
    _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态
    _mapView.showsUserLocation = YES;//显示定位图层
}

//跟随态
-(IBAction)startFollowing:(id)sender
{
    NSLog(@"进入跟随态");
    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;
    _mapView.showsUserLocation = YES;
}

//罗盘态
-(IBAction)startFollowHeading:(id)sender
{
    NSLog(@"进入罗盘态");
    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
    _mapView.showsUserLocation = YES;
}

- (void)onGetPoiResult:(NSArray *)poiResultList searchType:(int)type errorCode:(int)error
{
    if (error == BMKErrorOk) {
        BMKPoiResult *result = [poiResultList objectAtIndex:0];
        for (int i = 0; i < result.poiInfoList.count; i++) {
            BMKPoiInfo *poi = [result.poiInfoList objectAtIndex:i];
            BMKPointAnnotation *item = [[BMKPointAnnotation alloc] init];
            item.coordinate = poi.pt;
            item.title = poi.name;
            [_mapView addAnnotation:item];
            [item release];
            [_mapView setCenterCoordinate:poi.pt];
            [_mapView setZoomLevel:18];
            break;
        }
    }
}


@end
